<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>

<!-- datagrid数据表格 -->
<table id="rolemyDatagrid"></table>
<!-- datagrid数据表格==END== -->

<!-- 修改 -->
<div id="myUpdateDialogDivrole" class="easyui-dialog" data-options="closed:true, width: 600, height: 400">
    <form method="post" id="myFormrole">
        角色名：<input type="text" name="roleName" readonly="readonly" id="updateroleName"/><br/><br/>
        菜单权限：
        <%--树形表格--%>
        <table id="myDatagridrole" style="width:300px;height:300px"></table>
        <%--树形表格--%>
        <input type="button" value="修改" onclick="doUpdateSubmit()">
    </form>
</div>
<!-- 修改 ===END===-->

<script type="text/javascript">
    $(function () {

        /* datagrid展示数据 */
        $("#rolemyDatagrid").datagrid({
            url: '${pageContext.request.contextPath}/role/showAll',
            pagination: true,
            columns: [[
                {field: 'roleId', title: '序号'},
                {field: 'roleName', title: '角色名'},
                {
                    field: 'cmfzResourceList', title: '权限', formatter: function (value, row, index) {
                        var result="";
                        $.each(value, function(){
                            var resourceName=this["name"];
                            result+=(resourceName+" ");
                        });
                        return result;
                }
                }
            ]],
            /* 双击修改 */
            onDblClickRow:function (rowIndex,rowData){
                $('#myUpdateDialogDivrole').dialog("open");

                $("#updateroleName").val(rowData.roleName);

                $.ajax({
                    url:"${pageContext.request.contextPath}/resource/showSelected",
                    data:"roleName="+rowData.roleName,
                    type:"post",
                    dataType:"JSON",
                    async:false,
                    success:function(data){
                    $.each(data,function () {
                        console.info(this)
                        $('#myDatagridrole').treegrid("select",this+"");
                    });
                }
            });
            }
        });
        /* datagrid展示数据  结束 */

        /*树形表格数据展示*/
        $('#myDatagridrole').treegrid({
            url:'${pageContext.request.contextPath}/resource/queryResource',
            idField:'id',
            treeField:'name',
            singleSelect : false,
            columns:[[
                {checkbox:true},
                {field:'name',title:'资源名字',width:180}
            ]]
        });
    });

    /* 修改 */
    function doUpdateSubmit(){
        //alert(1234)
        var updateroleName=$("#updateroleName").val();
        //alert(updateroleName)

        var allSelectedRows=$("#myDatagridrole").treegrid("getSelections");
        var ids=new Array();

        for(var i=0;i<allSelectedRows.length;i++){
            ids[i]=allSelectedRows[i].id;
        }
        $.ajax({
            url:"${pageContext.request.contextPath}/resource/updateRoleResource",
            data:"ids="+ids+"&updateroleName="+updateroleName,
            type:"post",
            dataType:"JSON",
            success:function(data){
                //alert(data);
                if(data.flag){
                    $("#rolemyDatagrid").datagrid("reload");
                    $('#myUpdateDialogDivrole').dialog("close");
                }else{
                    $.messager.alert('提示框','修改失败，请确认！','warning');
                }
            }
        });

    }
    /* 修改 ===END===*/

</script>